package com.zzyl.serve.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.Dto.NursingPlanDto;
import com.zzyl.serve.VO.NursingPlanAllVo;
import com.zzyl.serve.VO.NursingPlanVo;
import com.zzyl.serve.VO.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //1.查询护理计划
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //2.查询护理项目计划关系
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectNursingProjectPlanList(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }


    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1，修改护理计划
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

            //2.修改护理计划和护理项目的关系
            //判断集合是否为空
            if (nursingPlanDto.getProjectPlans() != null && !nursingPlanDto.getProjectPlans().isEmpty()){
                //2.1.删除护理计划和护理项目的关系
                nursingProjectPlanMapper.deleteNursingProjectPlanById(nursingPlan.getId());
                //2.2.新增护计划和护理项目的关系
                int count = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(),nursingPlan.getId());
            }
           return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }


    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(NursingPlanDto nursingPlanDto) {
        //1.新增护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //2.批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());

        return count == 0 ? 0 : 1;
    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingPlanById(Long id) {
        try {
            //1.删除护理计划
            removeById(id);
            //2.删除护理计划计划关系
            nursingProjectPlanMapper.deleteNursingProjectPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanAllVo> selectAll() {
       return nursingPlanMapper.selectAll();
    }



}
